<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-07-24 12:12:40
 * @version $Id$
 */
namespace app\finance\controller;
use think\Request;
use think\File;
use think\Db;
use think\Config;
use think\Controller;

use Godok\Org\Auth;
use Godok\Org\FileManager;
class Upload extends Base {
    
    /**
     * 带裁剪  650x400
     * @return [type] [description]
     */
    function index(){
        $file = Request::instance()->file('file');

        if ( ! $file instanceof File) {
            $this->error('非法上传！');
        }
        if ( !$file->validate(['size'=>2000000,'ext'=>'jpg,png,gif'])->check() ) {
            $this->error($file->getError());
        }

        $path = '/uploads';
        if ( isset($this->uid) ) {
            $path .= '/'.'u'.$this->uid.'/';
        }
        $path .= date('Ymd');

        $image = \think\Image::open($file);

        $fielname = md5(microtime(true)).'.'.$image->type();
        $realpath = str_replace('/', DS, $_SERVER['DOCUMENT_ROOT'].$path);

        if (!is_dir($realpath)) {
            mkdir($realpath, 0755, true);
        }

        // $info = FileManager::save($file);
        // p($realpath.DS.$fielname);
        
        $info = $image->thumb(650,400,\think\Image::THUMB_CENTER)->save($realpath.DS.$fielname,null,100);

        $this->success('上传成功！','',Request::instance()->domain().$path.'/'.$fielname);

        // if ( $info ) {
        //     $this->success('上传成功！','',$info[1]);
        // } else {
        //     $this->error(FileManager::getError());
        // }
    }

    public function upimg()
    {
        $file = Request::instance()->file('file');

        if ( ! $file instanceof File) {
            $this->error('非法上传！');
        }
        if ( !$file->validate(['size'=>2000000,'ext'=>'jpg,png,gif'])->check() ) {
            $this->error($file->getError());
        }
        $info = FileManager::save($file);
        if ( $info ) {
            $this->success('上传成功！','',$info[1]);
        } else {
            $this->error(FileManager::getError());
        }
    }
}